package com.aiit.controller;

import com.aiit.pojo.LingJian;
import com.aiit.pojo.Merchant;
import com.aiit.pojo.User;
import com.aiit.pojo.order.PurchaseOrder;
import com.aiit.pojo.order.PurchaseOrderOut;
import com.aiit.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.*;

@Controller
@RequestMapping("/main/purchase")
public class PurchaseController {
    @Autowired
    ILingJianService lingJianService;
    @Autowired
    IMerchantService merchantService;
    @Autowired
    IUserService userService;
    @Autowired
    IPurchaseOrderService purchaseOrderService;
    @Autowired
    IPurchaseOrderOutService purchaseOrderOutService;


    @RequestMapping("/pmanagement")
    public String toPManagement(){
        return "/main/purchase/pmanagement";
    }
    //获取前端进货页面表单所需要数据
    @ResponseBody
    @RequestMapping("/login/pmanagement")
    public Map<String,Object> loginPManagement(){
        return getName(0);
    }
    public Map<String,Object> getName(Integer i){
        Map<String, Object> map = new HashMap<>();
        Set<String> set1 = new HashSet<>();
        Set<String> set2 = new HashSet<>();
        Set<String> set3 = new HashSet<>();
        if(i==1){
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryAll2();
            //获取名字存入map
            purchaseOrders.forEach(purchaseOrder -> set1.add(lingJianService.getOne(new QueryWrapper<LingJian>().eq("id",purchaseOrder.getLingjianId())).getName()));
            map.put("lingjianname",set1);
            purchaseOrders.forEach(purchaseOrder -> set2.add(merchantService.getOne(new QueryWrapper<Merchant>().eq("id",purchaseOrder.getMerchantId())).getName()));
            map.put("merchantname",set2);
            purchaseOrders.forEach(purchaseOrder -> set3.add(userService.getOne(new QueryWrapper<User>().eq("id",purchaseOrder.getUserId())).getName()));
            map.put("userName",set3);
        }else if (i==2){
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryAll3();
            //获取名字存入map
            purchaseOrders.forEach(purchaseOrder -> set1.add(lingJianService.getOne(new QueryWrapper<LingJian>().eq("id",purchaseOrder.getLingjianId())).getName()));
            map.put("lingjianname",set1);
            purchaseOrders.forEach(purchaseOrder -> set2.add(merchantService.getOne(new QueryWrapper<Merchant>().eq("id",purchaseOrder.getMerchantId())).getName()));
            map.put("merchantname",set2);
            purchaseOrders.forEach(purchaseOrder -> set3.add(userService.getOne(new QueryWrapper<User>().eq("id",purchaseOrder.getUserId())).getName()));
            map.put("userName",set3);
        }else if (i==0){
            List<LingJian> lingJians = lingJianService.queryAll();
            lingJians.forEach(lingJian -> set1.add(lingJian.getName()));
            List<Merchant> merchants = merchantService.queryAll();
            merchants.forEach(merchant -> set2.add(merchant.getName()));
            List<User> users = userService.queryAll2();
            users.forEach(user -> set3.add(user.getName()));
            map.put("lingjianname",set1);
            map.put("merchantname",set2);
            map.put("userName",set3);
        }
        System.out.println("NAME:"+map);
        return map;
    }
    //插入前端传入数据到数据库中
    @ResponseBody
    @RequestMapping("/insertPurchaseOrder")
    public Map<String,Integer> insertPurchaseOrder(@RequestParam("LingJianName")String LingJianName, @RequestParam("MerchantName")String MerchantName, @RequestParam("number")Integer number, @RequestParam("date")String date, @RequestParam("username")String username) throws ParseException {
        Map<String, Integer> map = new HashMap<>();

        LingJian lingjian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("name", LingJianName));
        if(lingjian.getNumber()+number>2000){
            map.put("result",2);
            return map;
        }
        if(lingjian.getState().equals("即将溢满")){
            map.put("result",3);
            return map;
        }
        Merchant merchant = merchantService.getOne(new QueryWrapper<Merchant>().eq("name", MerchantName));
        User user = userService.getOne(new QueryWrapper<User>().eq("name", username));
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setLingjianId(lingjian.getId());
        purchaseOrder.setMerchantId(merchant.getId());
        purchaseOrder.setUserId(user.getId());
        purchaseOrder.setNumber(number);
        Double dnumber = number.doubleValue();
        purchaseOrder.setTotalAmount(dnumber*lingjian.getPrice());
        purchaseOrder.setDeadline(date);
        System.out.println(purchaseOrder);
        Integer result = purchaseOrderService.insert(purchaseOrder);
        if (lingjian.getNumber()+number>1500){
            lingjian.setState("即将溢满");
            lingJianService.updateById(lingjian);
        }
        map.put("result",result);
//        map.put("result",1);
        return map;
    }

    @RequestMapping("/pmanagementorder")
    public String toPManagementOrder(){
        return "/main/purchase/pmanagementorder";
    }
    //加载前端查询栏所需要数据
    @ResponseBody
    @RequestMapping("/login/pmanagementorder")
    public Map<String,Object> loginPManagementOrder(){
        Map<String, Object> map = getName(1);
        List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryAll2();
        System.out.println("purchaseOrders:"+purchaseOrders);
        for(int i = 0;i<purchaseOrders.size();i++){
            LingJian lingjian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("id", purchaseOrders.get(i).getLingjianId()));
            purchaseOrders.get(i).setLingjianName(lingjian.getName());
            Merchant merchant = merchantService.getOne(new QueryWrapper<Merchant>().eq("id", purchaseOrders.get(i).getMerchantId()));
            purchaseOrders.get(i).setMerchantName(merchant.getName());
            User user = userService.getOne(new QueryWrapper<User>().eq("id", purchaseOrders.get(i).getUserId()));
            purchaseOrders.get(i).setUserName(user.getName());
        }
        System.out.println(purchaseOrders);
        map.put("purchaseOrders",purchaseOrders);
        System.out.println(map);
        return map;
    }
    //根据前端查询条件查询进货订单
    @ResponseBody
    @RequestMapping("/select/pmanagementorder")
    public Map<String,Object> selectPManagementOrder(@RequestParam("id")String id,@RequestParam("lingjianName")String lingJianName,@RequestParam("merchantName")String merchantName){
        Map<String, Object> map = new HashMap<>();
        if (!(id.equals("直接选择或搜索选择"))){//id选择了
            PurchaseOrder purchaseOrders = purchaseOrderService.queryById2(Integer.parseInt(id));
            map.put("purchaseOrders",toGetPurchaseOrder(purchaseOrders));
            map.put("result",1);
        }else if (!lingJianName.equals("直接选择或搜索选择") && !merchantName.equals("直接选择或搜索选择")){
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryByLMId(lingJianService.getOne(new QueryWrapper<LingJian>().eq("name",lingJianName)).getId(),merchantService.getOne(new QueryWrapper<Merchant>().eq("name",merchantName)).getId());
            map.put("purchaseOrders",toGetPurchaseOrders(purchaseOrders));
        }else if(!lingJianName.equals("直接选择或搜索选择")){
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryByLId(lingJianService.getOne(new QueryWrapper<LingJian>().eq("name", lingJianName)).getId());
            map.put("purchaseOrders",toGetPurchaseOrders(purchaseOrders));
        }else if(!merchantName.equals("直接选择或搜索选择")){
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryByMId(merchantService.getOne(new QueryWrapper<Merchant>().eq("name", merchantName)).getId());
            map.put("purchaseOrders",toGetPurchaseOrders(purchaseOrders));
        }else{
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryAll2();
            map.put("purchaseOrders",toGetPurchaseOrders(purchaseOrders));
        }
        System.out.println("Map:"+map);
        return  map;
    }
    public List<PurchaseOrder> toGetPurchaseOrders(List<PurchaseOrder> purchaseOrders){
        for(int i = 0;i<purchaseOrders.size();i++){
            LingJian lingjian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("id", purchaseOrders.get(i).getLingjianId()));
            purchaseOrders.get(i).setLingjianName(lingjian.getName());
            Merchant merchant = merchantService.getOne(new QueryWrapper<Merchant>().eq("id", purchaseOrders.get(i).getMerchantId()));
            purchaseOrders.get(i).setMerchantName(merchant.getName());
            User user = userService.getOne(new QueryWrapper<User>().eq("id", purchaseOrders.get(i).getUserId()));
            purchaseOrders.get(i).setUserName(user.getName());
        }
        return purchaseOrders;
    }
    public PurchaseOrder toGetPurchaseOrder(PurchaseOrder purchaseOrder){
            LingJian lingjian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("id", purchaseOrder.getLingjianId()));
            purchaseOrder.setLingjianName(lingjian.getName());
            Merchant merchant = merchantService.getOne(new QueryWrapper<Merchant>().eq("id", purchaseOrder.getMerchantId()));
            purchaseOrder.setMerchantName(merchant.getName());
            User user = userService.getOne(new QueryWrapper<User>().eq("id", purchaseOrder.getUserId()));
            purchaseOrder.setUserName(user.getName());
        return purchaseOrder;
    }
    //完成前端删除操作，删除对应信息
    @ResponseBody
    @RequestMapping("/delete/pmanagementorder")
    public Map<String,Object> selectPManagementOrder(Integer id){
        Map<String, Object> map = new HashMap<>();
        Integer result = purchaseOrderService.deleteById(id);
        map.put("result",result);
        return  map;
    }
    @RequestMapping("/omanagement")
    public String toOManagement(){
        return "/main/purchase/omanagement";
    }
    //获取前端信息退单原因，和退单订单id，更改订单的状态
    @ResponseBody
    @RequestMapping("/update/omanagement")
    public Map<String,Object> updateOManagement(@RequestParam("msg")String msg,@RequestParam("id")Integer id){
        Map<String, Object> map = new HashMap<>();
        purchaseOrderService.updateById2(id);
        PurchaseOrderOut purchaseOrderOut = new PurchaseOrderOut();
        purchaseOrderOut.setPurchaseOrderId(id);
        purchaseOrderOut.setTimeoutMsg(msg);
        PurchaseOrder purchaseOrder = purchaseOrderService.queryById3(id);
        LingJian lingJian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("id", purchaseOrder.getLingjianId()));
        Merchant merchant = merchantService.getOne(new QueryWrapper<Merchant>().eq("id", purchaseOrder.getMerchantId()));
        merchant.setTimeoutNumber(merchant.getTimeoutNumber()+1);
        merchant.setTotalNumber(merchant.getTotalNumber()+1);
        if (purchaseOrder.getTrueNumber()==null){
            Integer result = purchaseOrderOutService.insert(purchaseOrderOut);
            map.put("result",result);
            return map;
        }
        lingJian.setNumber(lingJian.getNumber()-purchaseOrder.getTrueNumber());
        if (lingJianService.updateById(lingJian)){
            Integer result = purchaseOrderOutService.insert(purchaseOrderOut);
            map.put("result",result);
        }else {
            Integer result = 2;
            map.put("result",result);
        }
        return map;
    }
    @RequestMapping("/omanagementorder")
    public String toOManagementOrder(){
        return "/main/purchase/omanagementorder";
    }
    //根据前端传递信息查找相关信息，传递到前端并进行数据处理
    @ResponseBody
    @RequestMapping("/select/omanagementorder")
    public Map<String,Object> selectOManagementOrder(@RequestParam("id")String id,@RequestParam("lingjianName")String lingJianName,@RequestParam("merchantName")String merchantName){
        Map<String, Object> map = new HashMap<>();

        if (!(id.equals("直接选择或搜索选择"))){//id选择了
            PurchaseOrder purchaseOrders = purchaseOrderService.queryById3(Integer.parseInt(id));
            PurchaseOrderOut purchaseOrderOut = purchaseOrderOutService.getOne(new QueryWrapper<PurchaseOrderOut>().eq("id",id));
            purchaseOrders.setMsg(purchaseOrderOut.getTimeoutMsg());
            purchaseOrders.setOutTime(purchaseOrderOut.getCreateTime());
            map.put("purchaseOrders",toGetPurchaseOrder(purchaseOrders));
            map.put("result",1);
        }else if (!lingJianName.equals("直接选择或搜索选择") && !merchantName.equals("直接选择或搜索选择")){
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryByLMId2(lingJianService.getOne(new QueryWrapper<LingJian>().eq("name",lingJianName)).getId(),merchantService.getOne(new QueryWrapper<Merchant>().eq("name",merchantName)).getId());
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setMsg(purchaseOrderOutService.getOne(new QueryWrapper<PurchaseOrderOut>().eq("purchase_order_id",purchaseOrder.getId())).getTimeoutMsg()));
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setOutTime(purchaseOrderOutService.getOne(new QueryWrapper<PurchaseOrderOut>().eq("purchase_order_id",purchaseOrder.getId())).getCreateTime()));
            map.put("purchaseOrders",toGetPurchaseOrders(purchaseOrders));
        }else if(!lingJianName.equals("直接选择或搜索选择")){
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryByLId2(lingJianService.getOne(new QueryWrapper<LingJian>().eq("name", lingJianName)).getId());
            System.out.println(purchaseOrders);
            System.out.println(purchaseOrderOutService.getOne(new QueryWrapper<PurchaseOrderOut>().eq("purchase_order_id", 17)).getTimeoutMsg());
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setMsg(purchaseOrderOutService.getOne(new QueryWrapper<PurchaseOrderOut>().eq("purchase_order_id",purchaseOrder.getId())).getTimeoutMsg()));
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setOutTime(purchaseOrderOutService.getOne(new QueryWrapper<PurchaseOrderOut>().eq("purchase_order_id",purchaseOrder.getId())).getCreateTime()));
            map.put("purchaseOrders",toGetPurchaseOrders(purchaseOrders));
        }else if(!merchantName.equals("直接选择或搜索选择")){
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryByMId2(merchantService.getOne(new QueryWrapper<Merchant>().eq("name", merchantName)).getId());
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setMsg(purchaseOrderOutService.getOne(new QueryWrapper<PurchaseOrderOut>().eq("purchase_order_id",purchaseOrder.getId())).getTimeoutMsg()));
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setOutTime(purchaseOrderOutService.getOne(new QueryWrapper<PurchaseOrderOut>().eq("purchase_order_id",purchaseOrder.getId())).getCreateTime()));
            map.put("purchaseOrders",toGetPurchaseOrders(purchaseOrders));
        }else{
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryAll3();
            System.out.println(purchaseOrders);
            if (!(purchaseOrders==null)){
                purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setMsg(purchaseOrderOutService.getOne(new QueryWrapper<PurchaseOrderOut>().eq("purchase_order_id",purchaseOrder.getId())).getTimeoutMsg()));
                purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setOutTime(purchaseOrderOutService.getOne(new QueryWrapper<PurchaseOrderOut>().eq("purchase_order_id",purchaseOrder.getId())).getCreateTime()));
                map.put("purchaseOrders",toGetPurchaseOrders(purchaseOrders));
            }
        }
        System.out.println("Map:"+map);
        return map;
    }
    //加载页面原始数据
    @ResponseBody
    @RequestMapping("/login/omanagementorder")
    public Map<String,Object> loginOManagementOrder(){
        Map<String, Object> map = getName(2);
        List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryAll3();
        System.out.println(purchaseOrders);
        for(int i = 0;i<purchaseOrders.size();i++){
            LingJian lingjian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("id", purchaseOrders.get(i).getLingjianId()));
            purchaseOrders.get(i).setLingjianName(lingjian.getName());
            Merchant merchant = merchantService.getOne(new QueryWrapper<Merchant>().eq("id", purchaseOrders.get(i).getMerchantId()));
            purchaseOrders.get(i).setMerchantName(merchant.getName());
            User user = userService.getOne(new QueryWrapper<User>().eq("id", purchaseOrders.get(i).getUserId()));
            purchaseOrders.get(i).setUserName(user.getName());
        }
        System.out.println(purchaseOrders);
        map.put("purchaseOrders",purchaseOrders);
        System.out.println(map);
        return map;
    }

}
